<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'adminproduct_type.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<link rel="stylesheet" type="text/css" href="lib/layui/css/layui.css"/>
	<script src="lib/layui/layui.js" type="text/javascript" charset="utf-8"></script>
  </head>
  
 <script type="text/html" id="toolbarPro">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="addPro">添加新闻类别</button>
    <button class="layui-btn layui-btn-sm" lay-event="dels">批量删除</button>
  </div>
</script>
<script type="text/html" id="barProduct">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
  <script>
 layui.use(["table","jquery","layer","form",], function() {
			var table = layui.table;
			var form=layui.form;
			var layer=layui.layer;
			var $=layui.jquery;
			
			
			//添加操作
			form.on('submit(addproduct)', function(data){
			//表单序列化，快速获取表单中所有name属性
			var str=$("#addproform").serialize()+"&order=3";
		  $.post('AdminNewTypeServlet',str,function(res){
		  	if(res.success){
		  		layer.closeAll();//关闭所有弹窗
		  		layer.msg(res.result.message);
		  		table.reload("productReload");//表格刷新
		  		$("#addproform")[0].reset();
		  		form.render();
		  	}else{
		  		layer.msg(res.result.message);
		  	}
		  },"json");
		  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		 });
			
			
			//头部工具栏，添加删除
		table.on('toolbar(producttable)', function(obj){
			    var checkStatus = table.checkStatus(obj.config.id);
			    switch(obj.event){
			      case 'addPro':
			         layer.open({
						  type: 1, 
						  content: $('#addproform') 
						  ,area: ['800px', '600px']
						  ,offset: ['100px']
						});
				
			      break;
			      //自定义头工具栏右侧图标 - 提示
			      case 'dels':
			        layer.alert('这是工具栏右侧自定义的一个图标按钮');
			      break;
			    };
			  });
			  
			
		table.on('tool(producttable)', function(obj) {
		  	var data = obj.data;
		  	//console.log(obj)
		  	if(obj.event === 'del') {
		  		layer.confirm('真的删除行么', function(index) {
		 			$.post('AdminNewTypeServlet',{
		 				nid:data.nid,
						order:5
		 			},function(res){
		 				if(res.result.success){
		 					layer.alert(res.result.success);
		 					table.reload("productReload");
		 				}else{
		 					layer.alert(res.result.success);
		 				}
		 			},"json");
		  		});
		  		
		  	}else if(obj.event === 'edit') {
		  		 layer.open({
					type: 1, 
					content:$("#updateproform"),
					area: ['800px', '600px'],
					offset: ['100px'],
					title:"您正在修改的是 "+data.ntype+"的信息"
					});
					 //给表单赋值
						form.val("updateproform", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
						  "ntype":data.ntype // "name": "value"
						 ,"pid":data.pid
						 ,"nid":data.nid
						});
					
					
					//表单监听提交	
				form.on('submit(updateproduct)', function(data){
					//表单序列化，快速获取表单中所有name属性
					var str=$("#updateproform").serialize()+"&order=4";
				  $.post('AdminNewTypeServlet',str,function(res){
				  	if(res.result.success){
				  		layer.closeAll();//关闭所有弹窗
				  		layer.msg(res.result.success);
				  		table.reload("productReload");//表格刷新
				  		$("#updateproform")[0].reset();//清空表单
				  		form.render();
				  	}else{
				  		layer.msg(res.result.success);
				  	}
				  },"json");
				  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
				 });
				
		  	}
  		});	
			  
			
  //方法级渲染
  
   table.render({
    elem: '#producttable'
    ,url: 'AdminNewTypeServlet'
    ,toolbar: '#toolbarPro' //开启头部工具栏，并为其绑定左侧模板
    ,cols: [[
      {checkbox: true, fixed: true}
      ,{field:'ntype', title: '新闻类型', width:150}
      //,{field:'pid', title: '资讯类型ID', width:150}
  
      ,{fixed: 'right', title:'操作', toolbar: '#barProduct',width: 150}
      
    ]]
    ,id: 'productReload'
    ,page: true
    
    ,where:{//条件限制
    	order:1//通过order进行判断
    }
  });
  
   active = {
    reload: function(){
      var pname = $('#pname');
      var iswheel = $('#iswheel');
      
      //alert(pname.val());
      //执行重载
      table.reload('productReload', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
          
            pname: pname.val(),
            iswheel: iswheel.val(),
            order:1

        }
      }, 'data');
    }
  };
  
  $('#search').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>
  <body>
  
<!-- 添加表单 -->
		<form class="layui-form" action="" lay-filter="addproform" id="addproform" style="display: none;">

			<div class="layui-form-item" >
			    <label class="layui-form-label" style="width:100px;">新闻类型名称</label>
			    <div class="layui-input-inline">
			      <input lay-verify="rolename|required" type="text" name="ntype" autocomplete="off" placeholder="请输入新闻类型" class="layui-input">
			    </div>
			</div>
			<!-- <div class="layui-form-item">
			    <label class="layui-form-label" style="width:100px;">资讯类型编号</label>
			    <div class="layui-input-inline">
			      <input lay-verify="rolename|required" type="text" name="pid" autocomplete="off" placeholder="请输入资讯类型编号" class="layui-input">
			    </div>

			</div> -->
		     <div class="layui-form-item">
			    <div class="layui-input-block">
			      <button type="submit" class="layui-btn" lay-submit="" lay-filter="addproduct">提交</button>
			      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
			    </div>
			</div>
		   </form>
		   
		   <!--  修改表单 -->
	  <form  class="layui-form" action="" lay-filter="updateproform" id="updateproform" style="display: none;">
				
		<input name="nid" style="display: none;" />
				
				<div class="layui-form-item">
				   <label class="layui-form-label" style="width:100px;">新闻类型名称</label>
				    <div class="layui-input-inline">
				      <input lay-verify="" type="text" name="ntype" autocomplete="off" placeholder="请输入新闻类型" class="layui-input">
				    </div>
			    </div>
			    
			    <!-- <div class="layui-form-item"> 
			   <label class="layui-form-label" style="width:100px;">商品类型编号</label>
				<div class="layui-input-inline" style="width:190px;">
					<input lay-verify="" type="text" name="pid" autocomplete="off" placeholder="请输入商品类型编号" class="layui-input">
				</div>
				</div> -->
			     <div class="layui-form-item">
				    <div class="layui-input-block">
				      <button type="submit" class="layui-btn" lay-submit="" lay-filter="updateproduct">提交</button>
				      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
				    </div>
				</div>
			   </form>
  <table class="layui-hide" id="producttable" lay-filter="producttable"></table>
  </body>
</html>
